package org.example.test.config;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@ComponentScan({ "org.example" })
@EnableWebMvc
@PropertySources({ @PropertySource("classpath:database.properties"), @PropertySource("classpath:application.properties") })
public class WebAppSpringConfiguration extends WebMvcConfigurerAdapter implements ServletContextAware {

	@Autowired
	private Environment env;

	private static final String VIEW_RESOLVER_PREFIX = "view.resolver.prefix";
	private static final String VIEW_RESOLVER_SUFFIX = "view.resolver.sufix";

	@Override
	public void setServletContext(ServletContext servletContext) {

	}

	// @Override
	// public void addResourceHandlers(ResourceHandlerRegistry registry) {
	//
	// registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
	// registry.addResourceHandler("/css/**").addResourceLocations("/css/");
	// registry.addResourceHandler("/images/**").addResourceLocations("/images/");
	// registry.addResourceHandler("/js/**").addResourceLocations("/js/");
	// registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
	// }

	@Bean
	public InternalResourceViewResolver internalResourceViewResolver() {

		InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();

		internalResourceViewResolver.setPrefix(env.getProperty(VIEW_RESOLVER_PREFIX));
		internalResourceViewResolver.setSuffix(env.getProperty(VIEW_RESOLVER_SUFFIX));
		internalResourceViewResolver.setViewClass(JstlView.class);

		return internalResourceViewResolver;
	}

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

		return new PropertySourcesPlaceholderConfigurer();
	}

	// @Bean
	// public CommonsMultipartResolver multipartResolver() {
	//
	// CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
	//
	// return multipartResolver;
	// }

}
